package com.hupu.jrs.widget

import android.annotation.TargetApi
import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.os.Build
import android.os.Bundle
import android.preference.ListPreference
import android.util.AttributeSet

/**
 * Created by sll on 15/4/27.
 */
class WrapperListPreference : ListPreference {

    private var mBuilder: AlertDialog.Builder? = null

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int,
                defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}

    override fun onPrepareDialogBuilder(builder: AlertDialog.Builder) {
        super.onPrepareDialogBuilder(builder)

        mBuilder = builder
    }

    @Throws(Exception::class)
    private fun getFieldValue(filed: String): String {
        val titleField = WrapperListPreference::class.java.superclass.superclass.getDeclaredField(filed)
        titleField.isAccessible = true
        return titleField.get(this).toString()
    }

    override fun showDialog(state: Bundle) {
        try {
            if (Build.VERSION.SDK_INT < 100) {
                val mEntries = entries
                val mEntriesStrArr = arrayOfNulls<String>(mEntries.size)
                for (i in mEntries.indices)
                    mEntriesStrArr[i] = mEntries[i].toString()

                val method = WrapperListPreference::class.java.superclass.getDeclaredMethod("getValueIndex")
                method.isAccessible = true
                val mClickedDialogEntryIndex = Integer.parseInt(method.invoke(this).toString())

                val dialogTitle = getFieldValue("mDialogTitle")
                val mNegativeButtonText = getFieldValue("mNegativeButtonText")

                AlertDialogWrapper.Builder(context).setTitle(dialogTitle)
                        .setSingleChoiceItems(mEntriesStrArr, mClickedDialogEntryIndex,
                                DialogInterface.OnClickListener { dialog, which ->
                                    try {
                                        val titleField = WrapperListPreference::class.java.superclass
                                                .getDeclaredField("mClickedDialogEntryIndex")
                                        titleField.isAccessible = true
                                        titleField.set(this@WrapperListPreference, which)
                                    } catch (e: Exception) {
                                    }

                                    this@WrapperListPreference.onClick(dialog, DialogInterface.BUTTON_POSITIVE)
                                    dialog.dismiss()
                                })
                        .setOnDismissListener(this@WrapperListPreference)
                        .setNegativeButton(mNegativeButtonText, this@WrapperListPreference)
                        .show()
            } else {
                super.showDialog(state)
            }
        } catch (e: Exception) {
            super.showDialog(state)
        }

    }
}
